/**
 * jims
 */
package com.jims.master.modules.med.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.med.entity.OrgVsProvince;
import com.jims.master.modules.med.service.OrgVsProvinceService;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.oper.service.PriceListService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.med.entity.HisVsMedical;
import com.jims.master.modules.med.service.HisVsMedicalService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 医保数据与his数据对照Controller
 * @author 陈晓阳
 * @version 2017-06-06
 */
@Controller
@RequestMapping(value = "${adminPath}/med/hisVsMedical")
public class HisVsMedicalController extends BaseController {

	@Autowired
	private HisVsMedicalService hisVsMedicalService;
    @Autowired
    private PriceListService priceListService;
    @Autowired
    private OrgVsProvinceService orgVsProvinceService;
	
	@ModelAttribute
	public HisVsMedical get(@RequestParam(required=false) String id) {
		HisVsMedical entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hisVsMedicalService.get(id);
		}
		if (entity == null){
			entity = new HisVsMedical();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index"})
	public String list(HisVsMedical hisVsMedical, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<HisVsMedical> page = new Page<HisVsMedical>();
        page = hisVsMedicalService.findPage(new Page<HisVsMedical>(request, response), hisVsMedical);
		model.addAttribute("page", page);
		return "modules/med/hisVsMedicalIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(Model model,String id,String type) {
        HisVsMedical hisVsMedical = null;
        if("0".equals(type)){//对照
            hisVsMedical = hisVsMedicalService.findByPriceId(id);
        }else if("1".equals(type)){//1修改
            hisVsMedical = hisVsMedicalService.get(id);
        }
        model.addAttribute("hisVsMedical", hisVsMedical);
        return "modules/med/hisVsMedicalForm";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(HisVsMedical hisVsMedical, Model model) {
	    StringData data=new StringData();
		int i=hisVsMedicalService.save(hisVsMedical);
        if(i>0){
            data.setCode("success");
            data.setData("保存医保数据与his数据对照成功");
        }else{
            data.setCode("error");
            data.setData("保存医保数据与his数据对照失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(HisVsMedical hisVsMedical) {
	    StringData data=new StringData();
		int i=hisVsMedicalService.delete(hisVsMedical);
        if(i>0){
            data.setCode("success");
            data.setData("删除医保数据与his数据对照成功");
        }else{
            data.setCode("error");
            data.setData("删除医保数据与his数据对照失败");
        }
		return data;
	}

    /**
     *医保价表自动补全
     * @param q
     * @param limit
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"automatic", ""})
    @ResponseBody
    public List<HisVsMedical>  automaticClinic(String q, String limit) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<HisVsMedical> listByParam = hisVsMedicalService.findListByParam(q, limit);
        return listByParam;
    }
}